<?php
declare (strict_types = 1);

namespace app\mobile\controller;
use think\facade\Request;
use think\facade\View;
use think\facade\Session;
use think\facade\Db;

use \app\common\Time;
class Auth extends \app\mobile\controller\Base
{
  public $uid,$user;
  public function initialize(){
    parent::initialize();

    
    if (empty($this->header['token'])) {
      $resp['code'] = 404;
      $resp['msg'] = '';
      echo json_encode($resp);
      exit();
    }
    // print_r($this->user);
    if (!$this->user) {
      $resp['code'] = 401;
      $resp['msg'] = 'token 错误';
      echo json_encode($resp);
      exit();
    }
    $this->uid = $this->user['id'];

    $dutime = Time::daysAgo(7);
    if ($this->user['token_time'] < $dutime) {
      // $resp['code'] = 401;
      // $resp['msg'] = 'token 已过期';
      // echo json_encode($resp);
      // exit();
    }

    
  }
  public function index()
  {
    return View::fetch();
  }


  public function uppic()
  {
    $image = request()->file('file');
    $type = input('type');
    try {
      validate(
        [
          'file' => 
          [
            'fileExt' => 'gif,jpg,png'
          ]
        ]
      )->check(['file' => $image]);
      
      $savename = \think\facade\Filesystem::putFile( 'public', $image);
      $url = Request::domain()."/upload/".$savename;

      return $this->doSuccess('ok',$url);
    } catch (\think\exception\ValidateException $e) {
      return $this->doError('ok',$e->getMessage());
    }
  }
}
